﻿using ShopingList.ViewMode;

namespace ShopingList
{
    public partial class MainPage : ContentPage
    {
        MainPageModel mode;
        public MainPage()
        {
            InitializeComponent();
            mode = BindingContext as MainPageModel;
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            double index = 0;
            foreach (var item in mode.listRight)
            {
             
                if (button.Text==item.spType)
                {
                    break;
                }
                index++;
            }
            if (mode.listRight.Count == 0) return;
            var h1 = scrollViewr.Height;
            var h2 = lvr.Height;
            var h3 = slr.Height;
            var value = (index / (double)mode.listRight.Count) * h2;
            await scrollViewr.ScrollToAsync(0, value, true);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolbarItem_Clicked(object sender, EventArgs e)
        {
            Navigation.PushAsync(new AddItem());
        }


        private void SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
        {
            var grid = sender as Grid;
            switch (e.Direction)
            {
                case SwipeDirection.Left:
                    if (grid.ColumnDefinitions.Count ==2) {
                        grid.ColumnDefinitions.Add(new ColumnDefinition()
                        {
                            Width=80
                        });
                        Button button = new Button();
                        button.Text = "删除";
                        button.BackgroundColor = Colors.Red;
                        button.BindingContext = grid.BindingContext;
                        button.Clicked += Button_Clicked1;
                        grid.Children.Add(button);
                        Grid.SetColumn(button, 2);
                    }
                    // Handle the swipe
                    break;
                case SwipeDirection.Right:
                    if (grid.ColumnDefinitions.Count == 3)
                    {
                        grid.ColumnDefinitions.RemoveAt(2);
                        foreach (var item in grid.Children)
                        {
                            if (item is Button)
                            {
                                grid.Children.Remove(item);
                                break;
                            }
                        }
                    }
                    // Handle the swipe
                    break;
                case SwipeDirection.Up:
                    // Handle the swipe
                    break;
                case SwipeDirection.Down:
                    // Handle the swipe
                    break;
            }
        }

        private void Button_Clicked1(object? sender, EventArgs e)
        {
            Button button = sender as Button;
            MainPageModelItem item = (MainPageModelItem)button.BindingContext;
            mode.Delete(item);
        }
    }

}
